import cryptoJs from 'crypto-js';

//DES加密
export const encrypt = (message) => {
    // const key  = 'deskhnck';  //密钥
    // const iv   = '88090265';
    let key=cryptoJs.enc.Utf8.parse("CenteKey")
    // console.log(cryptoJs.enc.Utf8.parse(iv))
    // const key = cryptoJs.enc.Hex.parse("deskhnck");
    // const iv = cryptoJs.enc.Hex.parse("88090265");
    // const cipher = cryptoJs.DES.encrypt(message, key, {
    //         iv: iv,
    //         mode: cryptoJs.mode.CBC
    //       });
    //     return cipher.toString();


    const cipher = cryptoJs.DES.encrypt(message, key,{
        iv:key,
        mode:cryptoJs.mode.CBC,
        padding: cryptoJs.pad.Pkcs7
    });
    // console.log('b64 cipher.ciphertext:'+cryptoJs.enc.Base64.stringify(cipher.ciphertext))
    // console.log('ciphertext.tostring(b64):'+cipher.ciphertext.toString(cryptoJs.enc.Base64))
    // console.log('btoa cipher.ciphertext:'+btoa(cipher.ciphertext))
    // console.log('cipher.toString: '+cipher.toString())
    // console.log('btoa cipher.toString: '+btoa(cipher.toString()))
    return cipher.ciphertext.toString(cryptoJs.enc.Base64)
    // var keyHex = cryptoJs.enc.Utf8.parse(key)
    // var option = { mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 }
    // var encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
    // return encrypted.ciphertext.toString()
}


//DES解密
export const decryptDes = (message, key) => {
    var keyHex = cryptoJs.enc.Utf8.parse(key)
    var decrypted = cryptoJs.DES.decrypt(
        {
            ciphertext: cryptoJs.enc.Hex.parse(message)
        },
        keyHex,
        {
            mode: cryptoJs.mode.ECB,
            padding: cryptoJs.pad.Pkcs7
        }
    )
    return decrypted.toString(cryptoJs.enc.Utf8)
}
